﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Properties;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormPrincStats : Form, ICharacterSaved
    {
        private Boolean saved = false,
                savedAsDone = false;
        private String charFileName = "*.xml";

        public FormPrincStats()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Mon showDialog() pour tenir compte de si on ouvre ou non un nouveau personnage
        /// </summary>
        /// <param name="parent">la fenetre parent</param>
        public void ShowDialog(Form parent, int status)
        {
            DialogResult frmclose = DialogResult.OK;

            comboBoxClass.Items.AddRange(Utils.classList.ToArray());
            comboBoxRace.Items.AddRange(Utils.racesList.ToArray());
            comboBoxPrincWeapon.Items.AddRange(Utils.weaponList.ToArray());

            switch (status)
            {
                case Utils.LOAD_STATE:
                    this.Text = "Édition de personnage : Principale";

                    frmclose = loadCharDataFromFile(status);
                    break;
                case Utils.NEW_STATE:
                    this.Text = "Création de personnage : Principale";

                    break;
                default:
                    MessageBox.Show(this, "État non supporté.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmclose = DialogResult.Abort;
                    break;
            }

            if (frmclose == DialogResult.OK)
            {
                labelSizeEffective.Text = Convert.ToString(Utils.charac.con + Utils.charac.str);
                base.ShowDialog(parent);
            }
            else
            {
                parent.Show();
                Dispose();
            }
        }

        private void FormPrincStats_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this, null, e);
        }

        #region EVENTS BUTTONS

        private void quitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Sauvegarde les donnees de la fenetre dans un fichier.
        /// </summary>
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (saved)
            {
                MessageBox.Show(this, "Données déjà suavegardé.", "Sauvegarde", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (!savedAsDone)
            {
                saveFileDialogCharacter.FileName = charFileName;
                if (saveFileDialogCharacter.ShowDialog() == DialogResult.OK)
                {
                    charFileName = saveFileDialogCharacter.FileName;
                    Utils.saveCharacterInfo(this, charFileName);
                }
            }
            else
            {
                Utils.saveCharacterInfo(this, charFileName);
            }
        }

        private void buttonNextPage_Click(object sender, EventArgs e)
        {
            Hide();
            new FormPFSpending().ShowDialog(this.Owner, this, Utils.getCurrentState(), charFileName, savedAsDone);
        }

        /// <summary>
        /// Affiche la fenetre de choix d'avantage si on a encore des pts de creation
        /// </summary>
        private void addAdvButton_Click(object sender, EventArgs e)
        {
            if (Utils.charac.nbCreation > 0)
            {
                Hide();
                new selectAdvDisadvForm().ShowDialog(Utils.ADVANTAGE, this);
            }
            else
            {
                MessageBox.Show("Vous n'avez plus de point de creation.", "Information",
                                                                MessageBoxButtons.OK,
                                                                MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Affiche la fenetre de choix de desavantage si on a encore des pts de desavantage
        /// </summary>
        private void addDisadvButton_Click(object sender, EventArgs e)
        {
            if (Utils.charac.nbDisadvantave > 0)
            {
                Hide();
                new selectAdvDisadvForm().ShowDialog(Utils.DISADVANTAGE, this);
            }
            else
            {
                MessageBox.Show("Vous n'avez plus de point de desavantage.", "Information",
                                                                MessageBoxButtons.OK,
                                                                MessageBoxIcon.Exclamation);
            }
        }

        #endregion

        #region EVENTS OF BOXES

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            Utils.charac.charName = textBoxName.Text;
            if (Utils.getCurrentState() != Utils.LOAD_STATE)
            {
                if (!savedAsDone)
                {
                    charFileName = textBoxName.Text + ".xml";
                }
            }
            dataChanged();
        }

        private void comboBoxSex_SelectedIndexChanged(object sender, EventArgs e)
        {
            Utils.charac.sex = comboBoxSex.SelectedIndex;
            dataChanged();
        }

        private void textBoxAge_TextChanged(object sender, EventArgs e)
        {
            int tempVal = Utils.getIntFromString(textBoxAge.Text, this);

            if (tempVal > 0)
            {
                Utils.charac.age = tempVal;
                dataChanged();
            }
        }

        private void textBoxOrigin_TextChanged(object sender, EventArgs e)
        {
            Utils.charac.origin = textBoxOrigin.Text;
            dataChanged();
        }

        private void textBoxHeight_TextChanged(object sender, EventArgs e)
        {
            int tempVal = Utils.getIntFromString(textBoxHeight.Text, this);

            if (tempVal > 0)
            {
                Utils.charac.height = tempVal;
                dataChanged();
            }
        }

        private void textBoxWeight_TextChanged(object sender, EventArgs e)
        {
            int tempVal = Utils.getIntFromString(textBoxWeight.Text, this);

            if (tempVal > 0)
            {
                Utils.charac.weight = tempVal;
                dataChanged();
            }
        }

        private void textBoxEyes_TextChanged(object sender, EventArgs e)
        {
            Utils.charac.eyes = textBoxEyes.Text;
            dataChanged();
        }

        private void textBoxHair_TextChanged(object sender, EventArgs e)
        {
            Utils.charac.hairs = textBoxHair.Text;
            dataChanged();
        }

        private void comboBoxRace_SelectedIndexChanged(object sender, EventArgs e)
        {
            Utils.charac.race = comboBoxRace.SelectedItem.ToString();
            dataChanged();
        }

        private void comboBoxClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            Utils.charac.charClass = comboBoxClass.SelectedItem.ToString();
            dataChanged();
        }



        private void comboBoxPrincWeapon_SelectedIndexChanged(object sender, EventArgs e)
        {
            Utils.charac.principalWeapon = Utils.weaponList[comboBoxPrincWeapon.SelectedIndex];
            dataChanged();
        }

        #endregion

        #region Local Method

        /// <summary>
        /// active le bouton de sauvegarde et flag comme non sauvegarde
        /// </summary>
        public void dataChanged()
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                saved = false;
                saveButton.Enabled = true;
                buttonNextPage.Enabled = Utils.isCharacValidForSkill();
            }
        }

        /// <summary>
        /// Affiche la fenetre d'ouverture de fichier puis charge le personnage selectionner
        /// </summary>
        /// <returns>le bouton clique dans la fenetre de choix de fichier</returns>
        private DialogResult loadCharDataFromFile(int state)
        {
            DialogResult frmclose = openFileDialogCharacter.ShowDialog();

            if (frmclose == DialogResult.OK)
            {
                charFileName = openFileDialogCharacter.FileName;
                //lit le fichier du personnage et le met dans la variable charac.
                Utils.xmlDeserializeCharacter(charFileName);

                //affiche les données lues en mode read only.
                textBoxName.Text = Utils.charac.charName;
                comboBoxSex.SelectedIndex = Utils.charac.sex;
                textBoxOrigin.Text = Utils.charac.origin;
                textBoxAge.Text = Convert.ToString(Utils.charac.age);
                textBoxHeight.Text = Convert.ToString(Utils.charac.height);
                textBoxWeight.Text = Convert.ToString(Utils.charac.weight);
                textBoxEyes.Text = Utils.charac.eyes;
                textBoxHair.Text = Utils.charac.hairs;
                numericUpDownAgi.Value = Utils.charac.agi;
                numericUpDownCon.Value = Utils.charac.con;
                numericUpDownDex.Value = Utils.charac.dex;
                numericUpDownStr.Value = Utils.charac.str;
                numericUpDownInt.Value = Utils.charac.intel;
                numericUpDownPer.Value = Utils.charac.per;
                numericUpDownPow.Value = Utils.charac.pow;
                numericUpDownWill.Value = Utils.charac.will;
                numericUpDownApp.Value = Utils.charac.app;

                comboBoxRace.SelectedIndex = Utils.racesList.IndexOf(new Race(Utils.charac.race));
                comboBoxClass.SelectedIndex = Utils.classList.IndexOf(new CharacterClass(Utils.charac.charClass));
                comboBoxPrincWeapon.SelectedIndex = Utils.weaponList.IndexOf(Utils.charac.principalWeapon);

                foreach (AdvDisadv adv in Utils.charac.advantage)
                {
                    avantageListBox.Items.Add(adv.name + " (" + adv.cost + ")");
                }

                foreach (AdvDisadv dis in Utils.charac.disadvantage)
                {
                    disadvantageListBox.Items.Add(dis.name + " (" + dis.cost + ")");
                }

                characterSaved(true);
                buttonNextPage.Enabled = Utils.isCharacValidForSkill();
            }

            return frmclose;
        }

        /// <summary>
        /// Ajoute le adv a la liste des avantage
        /// </summary>
        /// <param name="adv">l'avantage selectionne</param>
        public void addAdvantage(AdvDisadv adv)
        {
            if ((Utils.charac.nbCreation - adv.cost) >= 0)
            {
                avantageListBox.Items.Add(adv.name + " (" + adv.cost + ")");
                Utils.addAdvantageToCharac(adv);
                dataChanged();
            }
            else
            {
                MessageBox.Show("Vous n'avez pas assez de point de création.", "Information",
                                                                MessageBoxButtons.OK,
                                                                MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Ajoute les disadv a la liste de desavantage
        /// </summary>
        /// <param name="disadv">le desavantage selectionne</param>
        public void addDisadvantage(AdvDisadv disadv)
        {
            if ((Utils.charac.nbDisadvantave + disadv.cost) >= 0)
            {
                disadvantageListBox.Items.Add(disadv.name + " (" + disadv.cost + ")");
                Utils.addDisadvantageToCharac(disadv);
                dataChanged();
            }
            else
            {
                MessageBox.Show("Vous n'avez pas assez de point de désavantage.", "Information",
                                                                MessageBoxButtons.OK,
                                                                MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isSaved"></param>
        public void characterSaved(bool isSaved)
        {
            saved = isSaved;
            savedAsDone = isSaved;
            saveButton.Enabled = !isSaved;
        }

        #endregion
    }
}
